package nus.mtech.util;

import java.awt.event.KeyEvent;

import javax.swing.JTextField;

/**
 * Subclassing of JTextField to allow only numeric values
 * 
 * @author Alvin Lee
 */

public class IntegerTextField extends JTextField {

	public void processKeyEvent(KeyEvent ev) {
		char c = ev.getKeyChar();
		int keycode = ev.getKeyCode();
		if (Character.isDigit(c) || (c == 8) || (c == 127))
			super.processKeyEvent(ev);
		else if (keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_DOWN
				|| keycode == KeyEvent.VK_LEFT || keycode == KeyEvent.VK_RIGHT
				|| keycode == KeyEvent.VK_PASTE || keycode == KeyEvent.VK_HOME
				|| keycode == KeyEvent.VK_END || keycode == KeyEvent.VK_CUT)
			super.processKeyEvent(ev);
		else if (ev.isControlDown() || ev.isAltDown())
			super.processKeyEvent(ev);
		else
			ev.consume();
	}
}
